﻿using Microsoft.Extensions.Hosting;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace PluginSample
{
    public class HostService: BackgroundService
    {
        protected override async Task ExecuteAsync(CancellationToken stoppingToken)
        {
            while (true)
            {
                try
                {
                    stoppingToken.ThrowIfCancellationRequested();
                    Console.WriteLine(DateTime.Now.ToString("HH:mm:ss:ff"));
                    await Task.Delay(1000);
                }
                catch (OperationCanceledException)
                {
                    Console.WriteLine("收到停止信号,并停止.");
                    return;
                }
            }
        }
        //public Task StartAsync(CancellationToken cancellationToken)
        //{
        //    return Task.Run(async () =>
        //    {
        //        while (true)
        //        {
        //            try
        //            {
        //                Console.WriteLine(DateTime.Now.ToString("HH:mm:ss:ff"));
        //                await Task.Delay(1000);
        //                cancellationToken.ThrowIfCancellationRequested();
        //            }
        //            catch (OperationCanceledException)
        //            {
        //                Console.WriteLine("收到停止信号,并停止.");
        //                return;
        //            }
        //        }
        //    });
        //}


        //public Task StopAsync(CancellationToken cancellationToken)
        //{
        //    throw new NotImplementedException();
        //}
    }
}
